<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>WebSocket</h1>
<script>
    var webSocket = null;
    if ('WebSocket' in window) {
        webSocket = new WebSocket('ws://[[@{/webSocket}]]');
    } else {
        alert("该浏览器不支持WebSocket");
    }

    webSocket.onopen = function (event) {
        console.log('建立连接');
    };

    webSocket.onclose = function (event) {
        console.log('连接关闭');
    };
    
    webSocket.onmessage = function (event) {
        console.log('收到消息：' + event.data);
    };

    webSocket.onerror = function (event) {
        alert('webSocket通信发生错误！');
    };

    window.onbeforeunload = function () {
        webSocket.close();
    }
</script>
</body>
</html>